package com.zbkj.crmeb.merchant.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.constants.Constants;
import com.crmeb.core.common.CommonResult;
import com.utils.CrmebUtil;
import com.zbkj.crmeb.store.request.ChannelGroupBookingRequest;
import com.zbkj.crmeb.store.response.ChannelGroupBookingResponse;
import com.zbkj.crmeb.store.response.StoreProductGroupActivityResponse;
import com.zbkj.crmeb.store.service.ChannelGroupBookingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 功能描述: 拼团
 * @Author: wangzh
 * @Date: 2022/11/29 10:55
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/mer/group")
@Api(tags = "拼团")
public class MerGroupBookingController extends AdminBaseController {
    @Autowired
    private ChannelGroupBookingService channelGroupBookingService;

    /**
     * 功能描述: 传入所需数据进行新增、保存拼团信息
     * @Param: [channelGroupBookingRequest, request]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Boolean>
     * @Author: wangzh
     * @Date: 2022/11/29 15:05
     */
    @PostMapping
    @ApiOperation("保存/修改拼团活动")
    public CommonResult<Boolean> saveOrUpdateActivity(@RequestBody ChannelGroupBookingRequest channelGroupBookingRequest, HttpServletRequest request){
        return CommonResult.success(channelGroupBookingService.saveOrUpdateActivity(channelGroupBookingRequest, getMerId(request)));
    }

    /**
     * 功能描述: 传入活动id设置排序
     * @Param: [activityId, sort]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Boolean>
     * @Author: wangzh
     * @Date: 2022/11/29 17:49
     */
    @PutMapping("/{activityId}/{sort}")
    @ApiOperation("通过活动id设置排序")
    public CommonResult<Boolean> setActivitySort(@PathVariable Integer activityId, @PathVariable Integer sort){
        return CommonResult.success(channelGroupBookingService.setActivitySort(activityId, sort));
    }

    /**
     * 功能描述: 获取拼团列表 / 传入字段模糊搜索
     * @Param: [activityName, activityStatus, pageParamRequest, request]
     * @Return: com.crmeb.core.common.CommonResult<com.common.CommonPage<com.zbkj.crmeb.store.response.ChannelGroupBookingResponse>>
     * @Author: wangzh
     * @Date: 2022/11/29 17:49
     */
    @GetMapping
    @ApiOperation("获取拼团列表 / 传入活动名称、活动状态模糊查询")
    public CommonResult<CommonPage<ChannelGroupBookingResponse>> getList(String activityName, Integer activityStatus, String leaderName, String productName, PageParamRequest pageParamRequest, HttpServletRequest request){
        return CommonResult.success(CommonPage.restPage(channelGroupBookingService.getList(activityName, activityStatus, leaderName, pageParamRequest, getMerId(request), productName)));
    }

    /**
     * 功能描述: 传入拼团活动id获取基础信息（不包含拼团商品信息）
     * @Param: [activityId]
     * @Return: com.crmeb.core.common.CommonResult<com.zbkj.crmeb.store.response.ChannelGroupBookingResponse>
     * @Author: wangzh
     * @Date: 2022/11/30 9:52
     */
    @GetMapping("/{activityId}")
    @ApiOperation("根据活动id获取活动信息")
    public CommonResult<ChannelGroupBookingResponse> getByActivityId(@PathVariable Integer activityId, HttpServletRequest request){
        return CommonResult.success(channelGroupBookingService.getByActivityId(activityId, getMerId(request)));
    }

    /**
     * 功能描述: 获取商品信息
     * @Param: [productId]
     * @Return: com.crmeb.core.common.CommonResult<com.zbkj.crmeb.store.response.StoreProductGroupActivityResponse>
     * @Author: wangzh
     * @Date: 2022/12/1 16:55
     */
    @GetMapping("/product/{productId}/{groupId}")
    @ApiOperation("根据商品id获取商品信息")
    public CommonResult<StoreProductGroupActivityResponse> getProductInfoByProductId(@PathVariable Integer productId,@PathVariable String groupId,  HttpServletRequest request){
        log.info("根据商品id获取商品信息:{},{}", productId, groupId);
        if(groupId == null || "0".equals(groupId)){
            groupId = null;
        }
        return CommonResult.success(channelGroupBookingService.getProductInfoByProductId(productId, getMerId(request), false,groupId ));
    }

    /**
     * 功能描述: 根据id删除活动
     * @Param: [activityId]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Boolean>
     * @Author: wangzh
     * @Date: 2022/12/1 16:54
     */
    @DeleteMapping("/{activityId}")
    @ApiOperation("删除活动")
    public CommonResult<Boolean> removeActivityByActivityId(@PathVariable Integer activityId, HttpServletRequest request){
        return CommonResult.success(channelGroupBookingService.removeActivityByActivityId(activityId, getMerId(request)));
    }

    /**
     * 功能描述: 根据id设置活动结束时间
     * @Param: [activityId]
     * @Return: com.crmeb.core.common.CommonResult<java.lang.Boolean>
     * @Author: wangzh
     * @Date: 2022/12/1 16:54
     */
    @PutMapping("/ahead/{activityId}")
    @ApiOperation("提前结束")
    public CommonResult<Boolean> earlyTerminationActivity(@PathVariable Integer activityId, HttpServletRequest request){
        return CommonResult.success(channelGroupBookingService.earlyTerminationActivity(activityId, getMerId(request)));
    }

    /**
     * 功能描述: 根据团长id获取团长参与的拼团活动信息
     * @param leaderId
     * @return key:商品id  value: groupId
     */
    @GetMapping("/productId/{leaderId}")
    public CommonResult<List<ChannelGroupBookingResponse>> getProductByLeaderId(@PathVariable Integer leaderId, HttpServletRequest request){
        return CommonResult.success(channelGroupBookingService.getProductByLeaderId(leaderId));
    }
}
